package com.foton.lotteryinfrastructure.infrastru.repository;

import cn.hutool.core.util.ObjectUtil;
import com.foton.lotterydomain.activity.model.vo.DrawOrderVO;
import com.foton.lotterydomain.activity.model.vo.UserTakeActivityVO;
import com.foton.lotterydomain.activity.repository.IUserTakeActivityRepository;
import com.foton.lotteryinfrastructure.infrastru.dao.IUserStrategyExportDao;
import com.foton.lotteryinfrastructure.infrastru.dao.IUserTakeActivityCountDao;
import com.foton.lotteryinfrastructure.infrastru.dao.IUserTakeActivityDao;
import com.foton.lotteryinfrastructure.infrastru.po.UserStrategyExport;
import com.foton.lotteryinfrastructure.infrastru.po.UserTakeActivity;
import com.foton.lotteryinfrastructure.infrastru.po.UserTakeActivityCount;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author RenPu
 * @Date 2024/1/2 17:02
 * @Version 1.0
 * @Description: 用户活动业务实现类
 **/

@Component
public class UserTakeActivityRepository implements IUserTakeActivityRepository {


    @Resource
    private IUserTakeActivityCountDao userTakeActivityCountDao;

    @Resource
    private IUserTakeActivityDao userTakeActivityDao;

    @Resource
    private IUserStrategyExportDao iUserStrategyExportDao;

    @Override
    public int subtractionLeftCount(Long activityId, String activityName, Integer takeCount, Integer userTakeLeftCount, String uId, Date partakeDate) {

        if(null==userTakeLeftCount){
            UserTakeActivityCount userTakeActivityCount = new UserTakeActivityCount();
            userTakeActivityCount.setuId(Long.parseLong(uId));
            userTakeActivityCount.setActivityId(activityId);
            userTakeActivityCount.setTotalCount(takeCount);
            userTakeActivityCount.setLeftCount(takeCount-1);
            userTakeActivityCountDao.insert(userTakeActivityCount);
            return 1;
        }else {
            UserTakeActivityCount userTakeActivityCount = new UserTakeActivityCount();
            userTakeActivityCount.setuId(Long.parseLong(uId));
            userTakeActivityCount.setActivityId(activityId);
            return userTakeActivityCountDao.updateLeftCount(userTakeActivityCount);
        }


    }



    @Override
    public void takeActivity(Long activityId, String activityName, Integer takeCount, Integer userTakeLeftCount, String uId, Date takeDate, Long takeId) {

        UserTakeActivity userTakeActivity = new UserTakeActivity();
        userTakeActivity.setuId(uId);
        userTakeActivity.setTakeId(takeId);
        userTakeActivity.setActivityId(activityId);
        userTakeActivity.setActivityName(activityName);
        userTakeActivity.setTakeDate(takeDate);
        if (null == userTakeLeftCount) {
            userTakeActivity.setTakeCount(1);
        } else {
            userTakeActivity.setTakeCount(takeCount - userTakeLeftCount);
        }

        //设置去重ID，防止多次刷领
        String uuid = uId + "_" + activityId + "_" + userTakeActivity.getTakeCount();
        userTakeActivity.setUuid(uuid);

        userTakeActivityDao.insert(userTakeActivity);

    }

    @Override
    public UserTakeActivityVO queryNoConsumedTakeActivityOrder(Long acvityId, String uId) {
        UserTakeActivity userTakeActivity= userTakeActivityDao.queryNoConsumedTakeActivityOrder(new UserTakeActivity(uId,acvityId));
        if(ObjectUtil.isEmpty(userTakeActivity)){
            return null;
        }

        UserTakeActivityVO userTakeActivityVO = new UserTakeActivityVO();
        userTakeActivityVO.setActivityId(userTakeActivity.getActivityId());
        userTakeActivityVO.setTakeId(userTakeActivity.getTakeId());
        userTakeActivityVO.setStrategyId(userTakeActivity.getStrategyId());
        userTakeActivityVO.setState(userTakeActivity.getState());

        return userTakeActivityVO;
    }

    @Override
    public int lockTackActivity(String uId, Long activityId, Long takeId) {
        UserTakeActivity userTakeActivity =new UserTakeActivity();
        userTakeActivity.setActivityId(activityId);
        userTakeActivity.setuId(uId);
        userTakeActivity.setTakeId(takeId);
        return userTakeActivityDao.lockTackActivity(userTakeActivity);
    }

    @Override
    public void saveUserStrategyExport(DrawOrderVO drawOrder) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setuId(drawOrder.getuId());
        userStrategyExport.setActivityId(drawOrder.getActivityId());
        userStrategyExport.setOrderId(drawOrder.getOrderId());
        userStrategyExport.setStrategyId(drawOrder.getStrategyId());
        userStrategyExport.setStrategyMode(drawOrder.getStrategyMode());
        userStrategyExport.setGrantType(drawOrder.getGrantType());
        userStrategyExport.setGrantDate(drawOrder.getGrantDate());
        userStrategyExport.setGrantState(drawOrder.getGrantState());
        userStrategyExport.setAwardId(drawOrder.getAwardId());
        userStrategyExport.setAwardType(drawOrder.getAwardType());
        userStrategyExport.setAwardName(drawOrder.getAwardName());
        userStrategyExport.setAwardContent(drawOrder.getAwardContent());
        userStrategyExport.setUuid(String.valueOf(drawOrder.getOrderId()));
        iUserStrategyExportDao.insert(userStrategyExport);
    }

    @Override
    public void updateInvoiceMqState(String uId, Long orderId, Integer mqState) {
        UserStrategyExport userStrategyExport = new UserStrategyExport();
        userStrategyExport.setuId(uId);
        userStrategyExport.setOrderId(orderId);
        userStrategyExport.setMqState(mqState);
        iUserStrategyExportDao.updateInvoiceMqState(userStrategyExport);

    }
}
